<?php

namespace app\k50\controller;

use support\Request;
use Workerman\Protocols\Http\Chunk;
class PowerController
{

    //开启订单 
    public function startorder(Request $request)
    {
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $time = $request->post('time','');
        if(!$time){
            return json(['code'=>0,'msg'=>'缺失参数time']);
        }
        $dcl = $request->post('dcl',0);
        $mqtt = new \Workerman\Mqtt\Client('mqtt://47.103.32.234:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"shangxin6688"
        ));
        $mqtt->onConnect = function($mqtt) use($device_name,$time,$dcl){
            $mqtt->subscribe('CIOT/2000/DEVICE/'.$device_name);
            $data['Header'] = ['Code'=>'1004','Time'=>(time())*1000,'Sign'=>'lvzaina'];
            $data['Body'] = ['MsgId'=>time(),'Cmd'=>'Order','ID'=>time(),'Delay'=>(int)$time,'Type'=>0,'DCL'=>(int)$dcl];
            $str = json_encode($data,320);
            $mqtt->publish('CIOT/2000/HOST/'.$device_name, $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){

            $http_connection->send(new Chunk($content));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();  
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
    //提前关闭订单
    public function earlyendorder(Request $request)
    {
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $dcl = $request->post('dcl',0);
        $mqtt = new \Workerman\Mqtt\Client('mqtt://47.103.32.234:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"shangxin6688"
        ));
        $mqtt->onConnect = function($mqtt) use($device_name,$dcl){
            $mqtt->subscribe('CIOT/2000/DEVICE/'.$device_name);
            $data['Header'] = ['Code'=>'1004','Time'=>(time())*1000,'Sign'=>'lvzaina'];
            $data['Body'] = ['MsgId'=>time(),'Cmd'=>'Order','ID'=>time(),'Delay'=>0,'Type'=>0,'DCL'=>(int)$dcl];
            $str = json_encode($data,320);
            $mqtt->publish('CIOT/2000/HOST/'.$device_name, $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){

            $http_connection->send(new Chunk($content));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();  
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
    //打开继电器
    public function openpower(Request $request){
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $index = $request->post('index',1);
        $delay = $request->post('delay',0);
        $mqtt = new \Workerman\Mqtt\Client('mqtt://47.103.32.234:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"shangxin6688"
        ));
        $mqtt->onConnect = function($mqtt) use($device_name,$index,$delay){
            $mqtt->subscribe('CIOT/2000/DEVICE/'.$device_name);
            $data['Header'] = ['Code'=>'1004','Time'=>(time())*1000,'Sign'=>'lvzaina'];
            $data['Body'] = ['MsgId'=>time(),'Cmd'=>'Relay','Clear'=>1,'Relays'=>[['Index'=>(int)$index,'Delay'=>(int)$delay*1000]]];
            $str = json_encode($data,320);
            $mqtt->publish('CIOT/2000/HOST/'.$device_name, $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){

            $http_connection->send(new Chunk($content));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();  
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
    //开门指令(兼容蓝牙锁)
    public function opendoor(Request $request){
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $delay = $request->post('delay',5);
        $mqtt = new \Workerman\Mqtt\Client('mqtt://47.103.32.234:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"shangxin6688"
        ));
        $mqtt->onConnect = function($mqtt) use($device_name,$delay){
            $mqtt->subscribe('CIOT/2000/DEVICE/'.$device_name);
            $data['Header'] = ['Code'=>'1004','Time'=>(time())*1000,'Sign'=>'lvzaina'];
            $data['Body'] = ['MsgId'=>time(),'Cmd'=>'RemoteOpenDoor','ID'=>time(),'Delay'=>(int)$delay,'Type'=>0,'Lock'=>190];
            $str = json_encode($data,320);
            $mqtt->publish('CIOT/2000/HOST/'.$device_name, $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){

            $http_connection->send(new Chunk($content));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();  
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
    //语音播放指令(单条)
    public function playvoice(Request $request){
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $index = $request->post('index',1);
        $mqtt = new \Workerman\Mqtt\Client('mqtt://47.103.32.234:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"shangxin6688"
        ));
        $mqtt->onConnect = function($mqtt) use($device_name,$index){
            $mqtt->subscribe('CIOT/2000/DEVICE/'.$device_name);
            $data['Header'] = ['Code'=>'1004','Time'=>(time())*1000,'Sign'=>'lvzaina'];
            $data['Body'] = ['MsgId'=>time(),'Cmd'=>'Voice','Index'=>(int)$index];
            $str = json_encode($data,320);
            $mqtt->publish('CIOT/2000/HOST/'.$device_name, $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){

            $http_connection->send(new Chunk($content));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();  
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
    //语音播放指令(组合)
    public function playmorevoice(Request $request){
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $voices = $request->post('voices','');
        $voices = json_decode($voices,true);
        if($voices){
            foreach($voices as $k=>$v){
                $voices[$k]['Delay'] = $v['Delay']*5;
            }
        }
        $mqtt = new \Workerman\Mqtt\Client('mqtt://47.103.32.234:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"shangxin6688"
        ));
        $mqtt->onConnect = function($mqtt) use($device_name,$voices){
            $mqtt->subscribe('CIOT/2000/DEVICE/'.$device_name);
            $data['Header'] = ['Code'=>'1004','Time'=>(time())*1000,'Sign'=>'lvzaina'];
            $data['Body'] = ['MsgId'=>time(),'Cmd'=>'PlayVoice','Voices'=>$voices];
            $str = json_encode($data,320);
            $mqtt->publish('CIOT/2000/HOST/'.$device_name, $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){

            $http_connection->send(new Chunk($content));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();  
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
    //设置订单期间锁常开 门磁锁：设置订单联动开门并锁定
    public function magneticlock(Request $request){
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $mqtt = new \Workerman\Mqtt\Client('mqtt://47.103.32.234:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"shangxin6688"
        ));
        $mqtt->onConnect = function($mqtt) use($device_name){
            $mqtt->subscribe('CIOT/2000/DEVICE/'.$device_name);
            $data['Header'] = ['Code'=>'1003','Time'=>(time())*1000,'Sign'=>'lvzaina'];
            $data['Body'] = ['ConfVer'=>0,'4CM'=>1];
            $str = json_encode($data,320);
            $mqtt->publish('CIOT/2000/HOST/'.$device_name, $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){

            $http_connection->send(new Chunk($content));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();  
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
    //设置订单期间锁常开 蓝牙锁：开锁使用常开指令
    public function bluetoothlock(Request $request){
        $http_connection = $request->connection;
        $device_name = $request->post('device_name','');
        if(!$device_name){
            return json(['code'=>0,'msg'=>'缺失参数device_name']);
        }
        $mqtt = new \Workerman\Mqtt\Client('mqtt://47.103.32.234:1883', array(
            'debug' => true,
            "username"=>"admin", "password"=>"shangxin6688"
        ));
        $mqtt->onConnect = function($mqtt) use($device_name){
            $mqtt->subscribe('CIOT/2000/DEVICE/'.$device_name);
            $data['Header'] = ['Code'=>'1004','Time'=>(time())*1000,'Sign'=>'lvzaina'];
            $data['Body'] = ['MsgId'=>time(),'Cmd'=>'RemoteOpenDoor','ID'=>time(),'Delay'=>3,'Type'=>0,'Lock'=>189];
            $str = json_encode($data,320);
            $mqtt->publish('CIOT/2000/HOST/'.$device_name, $str);//向主题发送内容

        };
        $mqtt->onMessage = function($topic, $content) use($http_connection,$mqtt){

            $http_connection->send(new Chunk($content));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();  
        };
        $mqtt->onError = function(\Exception $exception=null) use($mqtt,$http_connection){
            $err = ['code'=>0,'msg'=>'连接出错'];
            $json = json_encode($err,320);
            $http_connection->send(new Chunk($json));
            $http_connection->send(new Chunk('')); // 发送空的的chunk代表response结束
            $mqtt->close();
        };
        $mqtt->connect();
        return response()->withHeaders([
            "Transfer-Encoding" => "chunked",
            "Content-Type"=>"application/json"
        ]);
    }
}
